﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Luna
{
    public class AppArgument
    {
        private List<string> args;
        private string[] switchPrefix = { "/", "-" };

        public AppArgument(string[] args)
        {
            this.args = new List<string>(args);
        }

		public string this[int index] {
			get { return args[index]; }
		}

        public String getFreeParamAfterSwitch(string @switch, string @default)
        {
            int idx = indexOf(@switch);
            return idx >= 0 && isFreeParam(idx + 1) ? args[idx + 1] : @default;
        }

        private bool isFreeParam(int p)
        {
            return (p >= 0 && p < args.Count && switchPrefix.findIndex((prefix) => args[p].StartsWith(prefix))  < 0) ;
        }

        private int indexOf(string @switch)
        {
			foreach (string prefix in switchPrefix)
			{
				int i = args.IndexOf(prefix + @switch);
				if (i > 0) return i;
			}
			return -1;
        }
    }
}
